//
//  STSignInViewController.swift
//  iPhone
//
//  Created by Toj on 4/19/23.
//

import UIKit

typealias STSignInCompletionBlock = (()->Void)

// MARK: - 懒加载(属性), 变量, 初始化init
class STSignInViewController: STBaseViewController {
    
    static let shared = STSignInViewController()
    
    private let viewModel = STSignInViewModel()
    
    /* STSignInCacheView */
    private lazy var cacheView: STSignInCacheView = {
        let view = STSignInCacheView()
        view.delegate = self
        view.isHidden = true
        return view
    }()
    
    /* STSignInView */
    private lazy var signInView: STSignInView = {
        let view = STSignInView()
        view.isHidden = true
        view.delegate = self
        return view
    }()
    
    private var _loginCompletion: STSignInCompletionBlock?
}

// MARK: - 生命周期
extension STSignInViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 数据请求
private extension STSignInViewController {
    
    func loginRequest() {
        
    }
}

// MARK: - 点击事件
private extension STSignInViewController {
    
}

// MARK: - Public
extension STSignInViewController {
    
    func show(complete: STSignInCompletionBlock?) {
        _loginCompletion = complete
        
        if parent == nil {
            let rootVC = UIApplication.shared.keyWindow?.rootViewController
            rootVC?.addChild(self)
            rootVC?.view.addSubview(view)
            view.frame = rootVC?.view.bounds ?? .fullScreen
        }
        
        view.isHidden = false
        
        let isCache = arc4random_uniform(2) == 1
        if isCache {
            cacheView.show()
            return
        }
        didTapOthers()
    }
    
    func dismiss() {
        guard cacheView.isHidden && signInView.isHidden else {
            return
        }
        _loginCompletion = nil
        view.isHidden = true
        
        // _loginCompletion?()
    }
    
    func didTapOthers() {
        signInView.show()
    }
}

// MARK: - Private
private extension STSignInViewController {
    
}

// MARK: - 代理事件
//extension STSignInViewController: <#Delegate#> {
//
//}

// MARK: - 初始化设置
private extension STSignInViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(cacheView)
        view.addSubview(signInView)
    }
    
    func setupMakeLayoutSubviews() {
        
        cacheView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(view)
        }
        
        signInView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = .clear
    }
}
